package com.hlf.modules.api.controller;

import com.hlf.common.annotation.SysLog;
import com.hlf.common.constants.Constants;
import com.hlf.common.utils.ReturnResult;
import com.hlf.common.validator.Assert;
import com.hlf.modules.api.annotation.LoginUser;
import com.hlf.modules.merchant.order.entity.Order;
import com.hlf.modules.merchant.order.service.OrderDetailService;
import com.hlf.modules.merchant.order.service.OrderService;
import com.hlf.modules.merchant.table.entity.Table;
import com.hlf.modules.merchant.table.service.TableService;
import com.hlf.modules.sys.entity.SysUserEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单管理接口
 */
@RestController
@RequestMapping("/api/order")
@Api("订单管理接口")
public class ApiOrderController{

    @Autowired
    private OrderService orderService;
    @Autowired
    private TableService tableService;

    /**
     * 创建订单
     */
    @SysLog("创建订单")
    @PostMapping("create")
    @ApiOperation(value = "创建订单", notes = "创建订单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "tableId", value = "餐桌id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult create(HttpServletRequest request, String tableId) {
        Assert.isBlank(tableId, "餐桌id不能为空");
        return orderService.createOrder(request, tableId);
    }

    /**
     * 确认菜品(加菜)
     */
    @SysLog("加菜订单")
    @PostMapping("add")
    @ApiOperation(value = "确认菜品(加菜)", notes = "确认菜品(加菜)")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "tableId", value = "餐桌id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult add(HttpServletRequest request, String tableId) {
        Assert.isBlank(tableId, "餐桌id不能为空");
        return orderService.addOrder(request, tableId);
    }

    /**
     * 备注
     */
    @SysLog("订单备注")
    @PostMapping("remarks")
    @ApiOperation(value = "备注", notes = "备注")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "orderId", value = "订单id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "remarks", value = "备注", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult remarks(HttpServletRequest request, String orderId, String remarks) {
        Assert.isBlank(orderId, "订单id不能为空");
        Assert.isBlank(remarks, "备注不能为空");
        //添加备注
        orderService.insertReamrks(request, orderId, remarks);
        return ReturnResult.ok();
    }

    /**
     * 取消订单
     */
    @SysLog("取消订单")
    @PostMapping("cancel")
    @ApiOperation(value = "取消订单", notes = "取消订单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "orderId", value = "订单id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult cancel(HttpServletRequest request, String orderId) {
        Assert.isBlank(orderId, "订单id不能为空");
        //取消订单
        Long tableId = orderService.cancelOrder(request, orderId);
        //查询餐桌
        Table table = tableService.queryObjectForId(tableId);
        Map<String, Object> map = new HashMap<>();
        map.put("date", new Date());
        map.put("tableNum", table.getTableNum());
        return ReturnResult.ok().put(Constants.DEFAULT_RESULT_NAME,map);
    }

    /**
     * 订单详情(总订单)
     */
    @PostMapping("orderList")
    @ApiOperation(value = "订单详情(总订单)", notes = "订单详情(总订单)")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "tableId", value = "餐桌id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult orderList(String tableId) {
        Assert.isBlank(tableId, "餐桌id不能为空");
        return orderService.orderList(tableId);
    }

    /**
     * 转台
     */
    @SysLog("转台")
    @PostMapping("change")
    @ApiOperation(value = "转台", notes = "转台")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "newTableId", value = "新选择餐桌id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "orderId", value = "订单id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult change(HttpServletRequest request, String newTableId, String orderId) {
        Assert.isBlank(orderId, "订单id不能为空");
        Assert.isBlank(newTableId, "新选择餐桌id不能为空");
        //转台
        orderService.changTable(request, newTableId, orderId);
        return ReturnResult.ok();
    }

    /**
     * 退菜
     */
    @SysLog("退菜")
    @PostMapping("returnGoods")
    @ApiOperation(value = "退菜", notes = "退菜")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "orderId", value = "订单id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "goodsId", value = "商品id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult returnGood(HttpServletRequest request, String orderId, String goodsId) {

        Assert.isBlank(orderId, "订单id不能为空");
        Assert.isBlank(goodsId, "商品id不能为空");
        orderService.returnGoods(request, orderId, goodsId);
        return ReturnResult.ok();
    }


    /**
     * 优惠折扣
     */
    @SysLog("订单使用优惠折扣")
    @PostMapping("chooseCoupon")
    @ApiOperation(value = "优惠折扣", notes = "优惠折扣")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "id", value = "优惠id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "orderId", value = "订单id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult chooseCoupon(HttpServletRequest request, String id, String orderId) {
        Assert.isBlank(id, "优惠id不能为空");
        Assert.isBlank(orderId, "订单id不能为空");

        orderService.rebate(request, id, orderId);
        return ReturnResult.ok();
    }

    /**
     * 打印小票
     */
    @PostMapping("print")
    @ApiOperation(value = "打印小票", notes = "打印小票")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "tableId", value = "餐桌id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult print(String tableId) {
        Assert.isBlank(tableId, "餐桌id不能为空");

        orderService.print(tableId);
        return ReturnResult.ok();
    }


    /**
     * 付款
     */

    @PostMapping("pay")
    @ApiOperation(value = "付款", notes = "付款")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "orderId", value = "订单id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult pay(String orderId) {
        Assert.isBlank(orderId, "订单id不能为空");

        Order order = orderService.queryForIdUnpaid(Long.parseLong(orderId));
        return ReturnResult.ok().put(Constants.DEFAULT_RESULT_NAME, order);
    }


    /**
     * 现金收款
     */
    @SysLog("现金付款")
    @PostMapping("cashPay")
    @ApiOperation(value = "现金收款", notes = "现金收款")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "orderId", value = "订单id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult cashPay(HttpServletRequest request, String orderId) {
        Assert.isBlank(orderId, "订单id不能为空");

        orderService.cashPay(request, orderId);
        return ReturnResult.ok();
    }

    /**
     * 一码收款
     */
    @SysLog("一码付款")
    @PostMapping("ymPay")
    @ApiOperation(value = "一码收款", notes = "一码收款")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "orderId", value = "订单id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult ymPay(HttpServletRequest request, String orderId) {
        Assert.isBlank(orderId, "订单id不能为空");

        orderService.ymPay(request, orderId);
        return ReturnResult.ok();
    }

    /**
     * 订单中心
     */
    @GetMapping("orderCenter")
    @ApiOperation(value = "订单中心", notes = "订单中心")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult orderCenter(@LoginUser SysUserEntity user) {
        Map<Byte, List<Order>> map = orderService.queryOrderByStoreId(user.getCurrentStoreId());
        return ReturnResult.ok().put(Constants.DEFAULT_RESULT_NAME, map);
    }


    /**
     * 订单搜索
     */
    @PostMapping("orderSearch")
    @ApiOperation(value = "订单搜索", notes = "订单搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "tableNum", value = "餐桌编号", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult orderSearch(String tableNum, @LoginUser SysUserEntity user) {
        Assert.isBlank(tableNum, "餐桌号不能为空");

        List<Order> orders = orderService.orderSerach(tableNum, user.getCurrentStoreId());
        if (orders.size() == 0) {
            return ReturnResult.ok();
        }
        return ReturnResult.ok().put(Constants.DEFAULT_RESULT_NAME, orders);
    }

    /**
     * 免茶位费
     */
    @SysLog("免茶位费")
    @PostMapping("gratisTea")
    @ApiOperation(value = "免茶位费", notes = "免茶位费")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "tableId", value = "餐桌id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult gratisTea(String tableId, @LoginUser SysUserEntity user) {
        Assert.isBlank(tableId, "餐桌id不能为空");

        orderService.gratisTea(tableId, user.getCurrentStoreId(),user);
        return ReturnResult.ok();
    }

    @Autowired
    OrderDetailService orderDetailService;
    /**
     * 今日订单数/营业额
     */
    @PostMapping("today")
    @ApiOperation(value = "今日订单数/营业额", notes = "今日订单数/营业额")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult today(@LoginUser SysUserEntity user) {
        return orderService.today(user);
    }

    /**
     * 订单中心所有(分页)
     */
    @PostMapping("orderAllPage")
    @ApiOperation(value = "订单中心所有(分页)", notes = "订单中心所有(分页)")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "page", value = "当前页码", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "limit", value = "每页多少条", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "orderSource", value = "订单来源 1:app  2:wechat", required = true)
    })
    public ReturnResult orderAllPage(@LoginUser SysUserEntity user,String page,String limit,String orderSource) {
        Assert.isNull(page,"当前页面不能为空!");
        Assert.isNull(limit,"每页条数不能为空!");
        Assert.isNull(orderSource,"订单来源不能为空!");

        Map<String, Object> map1 = new HashMap<>();
        map1.put("page",page);
        map1.put("storeId",user.getCurrentStoreId());
        map1.put("limit",limit);
        map1.put("orderSource",orderSource);
        List<Order> orderList = orderService.queryAllOrderPageByStoreId(map1);
        return ReturnResult.ok().put(Constants.DEFAULT_RESULT_NAME, orderList);
    }

    /**
     * 订单中心-根据状态(分页)
     */
    @PostMapping("orderStatusPage")
    @ApiOperation(value = "订单中心-根据状态(分页)", notes = "订单中心-根据状态(分页)")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "page", value = "当前页码", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "limit", value = "每页多少条", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "orderSource", value = "订单来源 1:app  2:wechat", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "payStatus", value = "支付状态 0待支付 1支付成功 2支付失败  3关闭订单", required = true)
    })
    public ReturnResult orderStatusPage(@LoginUser SysUserEntity user,String page,String limit,String orderSource,String payStatus) {
        Assert.isNull(page,"当前页面不能为空!");
        Assert.isNull(limit,"每页条数不能为空!");
        Assert.isNull(orderSource,"订单来源不能为空!");
        Assert.isNull(payStatus,"订单状态不能为空!");

        Map<String, Object> map1 = new HashMap<>();
        map1.put("page",page);
        map1.put("storeId",user.getCurrentStoreId());
        map1.put("limit",limit);
        map1.put("orderSource",orderSource);
        map1.put("payStatus",payStatus);
        List<Order> orderList = orderService.queryOrderPageByStoreId(map1);
        return ReturnResult.ok().put(Constants.DEFAULT_RESULT_NAME, orderList);
    }



}
